package my.example.samplegridview;

import my.example.samplegridview.modal.user;
import o2s.utils.imageHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class headerbarmain extends LinearLayout{

	private Activity act;
	public OrderPreviewAdapter opa;
	
	public headerbarmain(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.headerbarmain, this);
		
		Button backtoprevious = (Button) findViewById(R.id.mainback);
        backtoprevious.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				act.finish();
			}
        });
        
        Button backtocart = (Button) findViewById(R.id.mainbacktocart);
        backtocart.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent(act,cart.class);
				act.startActivity(i);
			}
        	
        });
        
        Button mainconfirmorder = (Button)findViewById(R.id.mainconfirmorder);
        mainconfirmorder.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				currentcartitems.sortingCurrentCartItem();
				
				final Dialog dialog = new Dialog(act);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.orderpreview);
				
				ListView lv = (ListView)dialog.findViewById(R.id.orderpreviewlist);
				
				LayoutInflater li = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				LinearLayout footer = (LinearLayout) li.inflate(R.layout.totalprice, null);
				TextView tv = (TextView)footer.findViewById(R.id.totalpriceprice);
				tv.setText("RM " + String.valueOf(currentcartitems.totalCartPrice()));
				lv.addFooterView(footer);
				
				opa = new OrderPreviewAdapter(act,currentcartitems.cartitems);
				lv.setAdapter(opa);
				
				Button backbutton = (Button)dialog.findViewById(R.id.orderpreviewback);
				backbutton.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						dialog.cancel();
					}
					
				});
				
				final Button confirmorder = (Button)dialog.findViewById(R.id.orderpreviewconfirm);
				confirmorder.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						if(currentcartitems.totalCartPrice() > 0){
							String alertMsg = "Thank you for the orders, we will now prepare your food :)";
							AlertDialog.Builder builder = new AlertDialog.Builder(act);
							builder.setTitle("Thank You!")
									.setMessage(alertMsg)
							       .setCancelable(false)
							       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
							           @Override
									public void onClick(DialogInterface dialog, int id) {
							        	   currentcartitems.turnAllProductStatus("Placed");
							        	   confirmorder.setVisibility(View.GONE);
							        	   opa.notifyDataSetChanged();
							              dialog.cancel();  
							           }
							       });
							AlertDialog alert = builder.create();
							alert.show();
						}
					}
				});
				
				if(currentcartitems.cartContainedUnOrderedItem()){
					confirmorder.setVisibility(View.VISIBLE);
				}
				else{
					confirmorder.setVisibility(View.GONE);
				}
				
				dialog.show();
			}
        	
        });
	
        Button switchuserbutton = (Button)findViewById(R.id.mainswitchuser);
		switchuserbutton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				
				final Dialog dialog = new Dialog(act);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.loginprompt);
				
				Button cancelbutton = (Button) dialog.findViewById(R.id.loginpromptcancel);
				cancelbutton.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						dialog.cancel();
					}
					
				});
				
				//Set Up GuestButton
				Button guestLogin = (Button)dialog.findViewById(R.id.loginpromptguestlogin);
				guestLogin.setVisibility(View.VISIBLE);
				guestLogin.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						currentuserdetail.currentuser = currentuserdetail.temporaryUserLookup("guest", "");
						LinearLayout ll = (LinearLayout)staticVar.menuStatic.findViewById(R.id.menuuserlayer);
						ll.setVisibility(View.GONE);
						Toast.makeText(act, "Welcome "+currentuserdetail.currentuser.getUseremail()+" ! You can now start ordering foods.", Toast.LENGTH_SHORT).show();
						dialog.cancel();
					}
					
				});

				Button submitbutton = (Button) dialog.findViewById(R.id.loginpromptsubmit);
				submitbutton.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						EditText et = (EditText) dialog.findViewById(R.id.loginpromptemailaddress);
						String emailadd = et.getText().toString();
						et = (EditText) dialog.findViewById(R.id.loginpromptpassword);
						String password = et.getText().toString();
						
						user u = currentuserdetail.temporaryUserLookup(emailadd, password);
						if(u != null){
							//Change back the menu user
							if(u.getUserImage().length() > 0){
								LinearLayout ll = (LinearLayout)staticVar.menuStatic.findViewById(R.id.menuuserlayer);
								ll.setVisibility(View.VISIBLE);
								
								ImageView iv = (ImageView)staticVar.menuStatic.findViewById(R.id.menuCustomerImage);
								Bitmap bm = imageHelper.getImageFromResource(staticVar.menuStatic, u.getUserImage());
								iv.setImageBitmap(bm);
							}
							
							TextView tv = (TextView)staticVar.menuStatic.findViewById(R.id.menuCustomerWelcomeText);
							tv.setText("Welcome "+emailadd);
							
							currentuserdetail.currentuser = u;
							Toast.makeText(act, "Welcome "+emailadd+" ! You can now start ordering foods.", Toast.LENGTH_SHORT).show();
						}
						else{
							Toast.makeText(act, "Failed to login. Please check your email address/password.", Toast.LENGTH_SHORT).show();
						}
						dialog.cancel();
					}
					
				});
				
				dialog.show();
			}
			
		});
	}
	
	public void reloadBatch(){
    	TextView tv = (TextView)findViewById(R.id.mainordereditembadge);
    	tv.setText(String.valueOf(currentcartitems.cartitems.size()));
    }
	
	public void setAct(Activity act){
		this.act = act;
	}

}
